package top.haijunit.navi.auth.domain;

import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.NullValueMappingStrategy;
import org.mapstruct.ReportingPolicy;
import org.springframework.util.CollectionUtils;
import top.haijunit.navi.auth.entity.PermissionEntity;
import top.haijunit.navi.auth.entity.PermissionGroupEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * 部门类型转换器
 *
 * @author zhanghaijun
 * @since 2024-10-25
 */
@Mapper(componentModel = "spring", unmappedTargetPolicy = ReportingPolicy.IGNORE, nullValueMappingStrategy = NullValueMappingStrategy.RETURN_DEFAULT)
public interface PermissionConvert {

    // 权限组类型转换
    @Mapping(target = "groupName", source = "groupName")
    @Mapping(target = "groupDesc", source = "remarks")
    PermissionGroupInfo convert(PermissionGroupEntity entity);

    @Mapping(target = "id", ignore = true)
    @Mapping(target = "createTime", ignore = true)
    @Mapping(target = "createBy", ignore = true)
    @Mapping(target = "updateTime", ignore = true)
    @Mapping(target = "updateBy", ignore = true)
    @Mapping(target = "deleted", ignore = true)
    @Mapping(target = "remarks", source = "groupDesc")
    PermissionGroupEntity convert(PermissionGroupInfo source);

    default List<PermissionGroupInfo> convertPermissionGroup(List<PermissionGroupEntity> sourceList) {
        return (CollectionUtils.isEmpty(sourceList) ? new ArrayList<>() : sourceList.stream().map(this::convert).toList());
    }

    // 权限类型转换
    @Mapping(target = "id", ignore = true)
    @Mapping(target = "createTime", ignore = true)
    @Mapping(target = "createBy", ignore = true)
    @Mapping(target = "updateTime", ignore = true)
    @Mapping(target = "updateBy", ignore = true)
    @Mapping(target = "deleted", ignore = true)
    @Mapping(target = "permissionId", source = "code")
    PermissionEntity convert(PermissionInfo permission);

}
